package com.xhwl.logistics.strategy.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.entity.VehicleArea;
import com.xhwl.logistics.service.cent.IAuthAccountService;
import com.xhwl.logistics.service.cent.IProjectService;
import com.xhwl.logistics.service.impl.VehicleAreaServiceImpl;
import com.xhwl.logistics.service.impl.VehicleCarServiceImpl;
import com.xhwl.logistics.service.impl.VehicleParkingLotServiceImpl;
import com.xhwl.logistics.service.impl.http.DaHuaHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotSyncHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

/**
 * 大华同步车辆信息
 */
@Slf4j
//@Service
public class VehicleParkingLotDaHuaIccSyncHandlerImpl implements IVehicleParkingLotSyncHandler {

    @Autowired
    private DaHuaHttpServiceImpl daHuaHttpService;

    @Autowired
    private VehicleCarServiceImpl vehicleCarService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private VehicleAreaServiceImpl vehicleAreaService;

    @Autowired
    private VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    private IAuthAccountService authAccountService;

    private Wrapper<VehicleArea> conditionForVehicleArea(String areaName,String remark,Integer orgId){
        LambdaQueryWrapper<VehicleArea> vehicleAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return vehicleAreaLambdaQueryWrapper.eq(VehicleArea::getAreaName,areaName)
                .eq(VehicleArea::getRemark,remark)
                .eq(VehicleArea::getOrgId,orgId);
    }

    @Override
    public void syncBlacklist(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        return;
    }

    @Override
    public void syncCardRule(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        return;
    }

    @Async("threadPoolTaskExecutorForSchedule")
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void syncCarList(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        //大华ICC已经有车辆授权功能，不需要车辆同步
        return;
//        log.info("KeTop sync car list start!");
//        log.info("KeTop sync car list vehicleIntegratedConfigBO: " + vehicleIntegratedConfigBO);
//
//        //调用大华接口，获取大华的车辆列表信息
//        Integer projectId = vehicleSyncBO.getProjectId();
//        List<CarInfoDaHuaDTO> daHuaCarList1 = daHuaHttpService.getDaHuaCarList(vehicleIntegratedConfigBO);
//        List<CarInfoDaHuaDTO> daHuaCarList = new LinkedList<>();
//        //去除黑名单车辆
//        if(daHuaCarList1!=null){
//            daHuaCarList = daHuaCarList1.stream().filter(s -> {
//                if (s.getListType() != 2) {
//                    return true;
//                } else {
//                    return false;
//                }
//            }).collect(Collectors.toList());
//        }
//        ProjectQuery projectQuery = new ProjectQuery();
//        List<Integer> ids = new LinkedList<>();
//        ids.add(projectId);
//        projectQuery.setIds(ids);
//        Project project = projectService.getOne(projectQuery, null);
//        //如果项目为空，则结束同步程序
//        if(project==null){
//            return;
//        }
//        //获取车行中台中的所有车辆信息
//        List<VehicleCar> vehicleCarList = vehicleCarService.list(vehicleCarService.conditionFotQueryAliveCars(projectId));
//        //如果大华获取的车辆信息不为空，车行中台获取的车辆信息也不为空。
//        if(daHuaCarList!=null&&!vehicleCarList.isEmpty()){
//            //根据车牌号获取大华的List集合和Map集合
//            List<String> plateNumberFromDaHuaIcc = daHuaCarList.stream().map(s -> s.getCarNum()).collect(Collectors.toList());
//            Map<String, CarInfoDaHuaDTO> mapForCarInfoDaHuaDTO = daHuaCarList.stream().collect(Collectors.toMap(s -> s.getCarNum(), Function.identity()));
//            //根据车牌号获取车行中台的List集合和Map集合
//            List<String> plateNumberFromCentVehicle = vehicleCarList.stream().map(s -> s.getPlateNumber()).collect(Collectors.toList());
//            Map<String, VehicleCar> mapForVehicleCar = vehicleCarList.stream().collect(Collectors.toMap(s -> s.getPlateNumber(), Function.identity()));
//            //获取需要新增的车辆信息
//            List<String> add = ListUtils.subtract(plateNumberFromDaHuaIcc, plateNumberFromCentVehicle);
//            //将需要新增的车辆信息加入到数据库中
//            if(!add.isEmpty()){
//                List<CarInfoDaHuaDTO> carInfoDaHuaDTOS = add.stream().map(mapForCarInfoDaHuaDTO::get).collect(Collectors.toList());
//                List<VehicleCar> vehicleCars = new LinkedList<>();
//                for (CarInfoDaHuaDTO carInfoDaHuaDTO : carInfoDaHuaDTOS) {
//                    VehicleCar vehicleCar = new VehicleCar();
//                    vehicleCar.setProjectId(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId());
//                    vehicleCar.setProjectName(project.getName());
//                    VehicleArea vehicleArea = vehicleAreaService.getOne(conditionForVehicleArea(carInfoDaHuaDTO.getParkingLot(), carInfoDaHuaDTO.getParkinglotCode(), project.getOrganizationId()));
//                    vehicleCar.setParkingLotId(vehicleArea.getParkingLotId());
//                    VehicleParkingLot vehicleParkingLot = vehicleParkingLotService.getById(vehicleArea.getParkingLotId());
//                    vehicleCar.setParkingLotName(vehicleParkingLot.getParkingLotName());
//                    vehicleCar.setPlateNumber(carInfoDaHuaDTO.getCarNum());
//                    vehicleCar.setCarColor(carInfoDaHuaDTO.getCarNumColorStr());
//                    vehicleCar.setCarType(convertDaHuaCarType(Integer.parseInt(carInfoDaHuaDTO.getCarType())));
//                    List<String> openIds = new LinkedList<>();
//                    openIds.add(carInfoDaHuaDTO.getOwnerId());
//                    List<AuthAccountMapping> authAccountMappings = authAccountService.listByOpenIds(openIds);
//                    if(!authAccountMappings.isEmpty()){
//                        for (AuthAccountMapping authAccountMapping : authAccountMappings) {
//                            if(StringUtils.equals(authAccountMapping.getProjectId().toString(),project.getId().toString())){
//                                vehicleCar.setOwnerAccountId(authAccountMapping.getAccountId());
//                            }
//                        }
//                    }
//                    vehicleCar.setOwnerName(carInfoDaHuaDTO.getOwner());
//                    OwnerInfoDaHuaDTO daHuaOwner = daHuaHttpService.getDaHuaOwner(vehicleIntegratedConfigBO, carInfoDaHuaDTO.getOwnerId());
//                    if(daHuaOwner!=null){
//                        vehicleCar.setOwnerPhone(daHuaOwner.getOwnerTel());
//                    }
//                    vehicleCar.setCreatorAccountName("xxl-job");
//                    vehicleCars.add(vehicleCar);
//                }
//                vehicleCarService.saveBatch(vehicleCars);
//            }
//            List<String> delete = ListUtils.subtract(plateNumberFromCentVehicle, plateNumberFromDaHuaIcc);
//            if(!delete.isEmpty()){
//                List<VehicleCar> vehicleCars = delete.stream().map(mapForVehicleCar::get).collect(Collectors.toList());
//                List<Integer> carIds = vehicleCars.stream().map(s -> s.getId()).collect(Collectors.toList());
//                vehicleCarService.removeByIds(carIds);
//            }
//            List<String> same = ListUtils.subtract(plateNumberFromCentVehicle, delete);
//            if(!same.isEmpty()){
//                List<VehicleCar> vehicleCars = new LinkedList<>();
//                for (String plateNumber : same) {
//                    VehicleCar vehicleCar = mapForVehicleCar.get(plateNumber);
//                    CarInfoDaHuaDTO carInfoDaHuaDTO = mapForCarInfoDaHuaDTO.get(plateNumber);
//                    if(StringUtils.equals(vehicleCar.getCarColor(),carInfoDaHuaDTO.getCarNumColorStr())&&StringUtils.equals(vehicleCar.getCarType().toString(),convertDaHuaCarType(Integer.parseInt(carInfoDaHuaDTO.getCarType())).toString())&&StringUtils.equals(vehicleCar.getOwnerName(),carInfoDaHuaDTO.getOwner())){
//
//                    }else{
//                        vehicleCar.setCarColor(carInfoDaHuaDTO.getCarNumColorStr());
//                        vehicleCar.setCarType(convertDaHuaCarType(Integer.parseInt(carInfoDaHuaDTO.getCarType())));
//                        vehicleCar.setOwnerName(carInfoDaHuaDTO.getOwner());
//                        vehicleCars.add(vehicleCar);
//                    }
//                }
//                vehicleCarService.updateBatchById(vehicleCars);
//            }
//        }
//        //如果大华的车辆列表为空，车行中台的车辆列表不为空。
//        else if(daHuaCarList==null&&!vehicleCarList.isEmpty()){
//            List<Integer> carIds = vehicleCarList.stream().map(vehicleCar -> vehicleCar.getId()).collect(Collectors.toList());
//            vehicleCarService.removeByIds(carIds);
//        }
//        //如果大华的车辆列表不为空，车行中台的车辆列表为空。
//        else if(daHuaCarList!=null&&vehicleCarList.isEmpty()){
//            List<VehicleCar> vehicleCars = new LinkedList<>();
//            for (CarInfoDaHuaDTO carInfoDaHuaDTO : daHuaCarList) {
//                VehicleCar vehicleCar = new VehicleCar();
//                vehicleCar.setProjectId(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId());
//                vehicleCar.setProjectName(project.getName());
//                VehicleArea vehicleArea = vehicleAreaService.getOne(conditionForVehicleArea(carInfoDaHuaDTO.getParkingLot(), carInfoDaHuaDTO.getParkinglotCode(), project.getOrganizationId()));
//                vehicleCar.setParkingLotId(vehicleArea.getParkingLotId());
//                VehicleParkingLot vehicleParkingLot = vehicleParkingLotService.getById(vehicleArea.getParkingLotId());
//                vehicleCar.setParkingLotName(vehicleParkingLot.getParkingLotName());
//                vehicleCar.setPlateNumber(carInfoDaHuaDTO.getCarNum());
//                vehicleCar.setCarColor(carInfoDaHuaDTO.getCarNumColorStr());
//                vehicleCar.setCarType(convertDaHuaCarType(Integer.parseInt(carInfoDaHuaDTO.getCarType())));
//                List<String> openIds = new LinkedList<>();
//                openIds.add(carInfoDaHuaDTO.getOwnerId());
//                List<AuthAccountMapping> authAccountMappings = authAccountService.listByOpenIds(openIds);
//                if(!authAccountMappings.isEmpty()){
//                    for (AuthAccountMapping authAccountMapping : authAccountMappings) {
//                        if(StringUtils.equals(authAccountMapping.getProjectId().toString(),project.getId().toString())){
//                            vehicleCar.setOwnerAccountId(authAccountMapping.getAccountId());
//                        }
//                    }
//                }
//                vehicleCar.setOwnerName(carInfoDaHuaDTO.getOwner());
//                OwnerInfoDaHuaDTO daHuaOwner = daHuaHttpService.getDaHuaOwner(vehicleIntegratedConfigBO, carInfoDaHuaDTO.getOwnerId());
//                if(daHuaOwner!=null){
//                    vehicleCar.setOwnerPhone(daHuaOwner.getOwnerTel());
//                }
//                vehicleCar.setCreatorAccountName("xxl-job");
//                vehicleCars.add(vehicleCar);
//            }
//            vehicleCarService.saveBatch(vehicleCars);
//        }
//        else{
//            //不需要做任何操作。
//        }
    }

    /**
     * 将大华的车类型转换为车行中台的车类型
     * @param carType
     * @return
     */
    private Integer convertDaHuaCarType(Integer carType){
        switch (carType){
            case 1:
            case 3:
            case 4:
            case 5:
            case 6:
            case 10:
            case 11:
                return 1;
            case 2:
                return 2;
            case 12:
                return 3;
            case 13:
            case 14:
            case 15:
            case 16:
            case 17:
                return 5;
            default:
                return null;
        }
    }

}
